<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>大数据之多范式编程语言 Scala05_函数式编程 | Oct25-X</title><meta name="keywords" content="Scala"><meta name="author" content="Oct25-X"><meta name="copyright" content="Oct25-X"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="函数式编程-函数基础一、函数基本语法 Scala 函数示例 1234567891011121314151617181920212223package org.example.chapter05&#x2F;** * @Description TODO Scala 函数 *&#x2F;object Scala01_Function &amp;#123;  def main(args: Array[String]): Unit &#x3D;">
<meta property="og:type" content="article">
<meta property="og:title" content="大数据之多范式编程语言 Scala05_函数式编程">
<meta property="og:url" content="https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/index.html">
<meta property="og:site_name" content="Oct25-X">
<meta property="og:description" content="函数式编程-函数基础一、函数基本语法 Scala 函数示例 1234567891011121314151617181920212223package org.example.chapter05&#x2F;** * @Description TODO Scala 函数 *&#x2F;object Scala01_Function &amp;#123;  def main(args: Array[String]): Unit &#x3D;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt48.png">
<meta property="article:published_time" content="2024-02-20T12:05:11.000Z">
<meta property="article:modified_time" content="2024-02-24T11:05:57.403Z">
<meta property="article:author" content="Oct25-X">
<meta property="article:tag" content="Scala">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt48.png"><link rel="shortcut icon" href="/oct25-xxxxx/img/logo.jpg"><link rel="canonical" href="https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/oct25-xxxxx/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/oct25-xxxxx/',
  algolia: undefined,
  localSearch: {"path":"search.json","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '大数据之多范式编程语言 Scala05_函数式编程',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-24 19:05:57'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/rightMenu.css"><meta name="generator" content="Hexo 6.1.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg22.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/oct25-xxxxx/">Oct25-X</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">大数据之多范式编程语言 Scala05_函数式编程</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-02-20T12:05:11.000Z" title="发表于 2024-02-20 20:05:11">2024-02-20</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-24T11:05:57.403Z" title="更新于 2024-02-24 19:05:57">2024-02-24</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/oct25-xxxxx/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="大数据之多范式编程语言 Scala05_函数式编程"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="函数式编程-函数基础"><a href="#函数式编程-函数基础" class="headerlink" title="函数式编程-函数基础"></a>函数式编程-函数基础</h1><h2 id="一、函数基本语法"><a href="#一、函数基本语法" class="headerlink" title="一、函数基本语法"></a>一、函数基本语法</h2><p><img src="/oct25-xxxxx/img/scala/09_%E5%87%BD%E6%95%B0%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95.png"></p>
<p>Scala 函数示例</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter05</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala 函数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala01_Function</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="comment">//调用函数</span></span><br><span class="line">    <span class="keyword">val</span> res: <span class="type">Int</span> = sum(<span class="number">2</span>, <span class="number">6</span>)</span><br><span class="line">    println(res)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * Scala 使用 def 定义一个函数</span></span><br><span class="line"><span class="comment">   *</span></span><br><span class="line"><span class="comment">   * @param x</span></span><br><span class="line"><span class="comment">   * @param y</span></span><br><span class="line"><span class="comment">   * @return</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">sum</span> </span>(x: <span class="type">Int</span>, y: <span class="type">Int</span>): <span class="type">Int</span> = &#123;</span><br><span class="line">    x + y</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="二、函数和方法的区别"><a href="#二、函数和方法的区别" class="headerlink" title="二、函数和方法的区别"></a>二、函数和方法的区别</h2><h3 id="核心概念"><a href="#核心概念" class="headerlink" title="核心概念"></a>核心概念</h3><ol>
<li><p>为完成某一功能的程序语句的集合，称为函数。</p>
</li>
<li><p><code>类中的函数称之方法。</code></p>
</li>
</ol>
<h3 id="函数和方法示例"><a href="#函数和方法示例" class="headerlink" title="函数和方法示例"></a>函数和方法示例</h3><ol>
<li><p><strong>函数没有重载和重写</strong>的概念；<strong>方法可以进行重载和重写</strong></p>
</li>
<li><p><strong>Scala 中函数可以嵌套定义</strong></p>
</li>
</ol>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter05</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala中函数和方法的区别</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala02_FunctioniAndMethod</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 定义在类中的函数就是方法</span></span><br><span class="line"><span class="comment">   * 方法可以重载和重写</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">f1</span></span>(): <span class="type">Unit</span> = &#123;</span><br><span class="line">    println(<span class="string">&quot;f1 ... ... &quot;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">f1</span></span>(s: <span class="type">String</span>): <span class="type">Unit</span> = &#123;</span><br><span class="line">    println(<span class="string">&quot;f1 ... overload ... &quot;</span>)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 函数没有重载和重写的概念</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f2</span></span>(): <span class="type">Unit</span> = &#123;</span><br><span class="line">      println(<span class="string">&quot;f2 ... ... &quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//报错</span></span><br><span class="line"><span class="comment">//    def f2(s: String): Unit = &#123;</span></span><br><span class="line"><span class="comment">//      println(&quot;f2 ... overload ... &quot;)</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 函数可以嵌套定义</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">f3</span></span>(): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f4</span></span>(): <span class="type">Unit</span> = &#123;</span><br><span class="line">      println(<span class="string">&quot;f3 f4 ... ...&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="三、函数定义"><a href="#三、函数定义" class="headerlink" title="三、函数定义"></a>三、函数定义</h2><h3 id="函数定义"><a href="#函数定义" class="headerlink" title="函数定义"></a>函数定义</h3><ol>
<li><p>函数1：无参，无返回值</p>
</li>
<li><p>函数2：无参，有返回值</p>
</li>
<li><p>函数3：有参，无返回值</p>
</li>
<li><p>函数4：有参，有返回值</p>
</li>
<li><p>函数5：多参，无返回值</p>
</li>
<li><p>函数6：多参，有返回值</p>
</li>
</ol>
<h3 id="函数定义示例"><a href="#函数定义示例" class="headerlink" title="函数定义示例"></a>函数定义示例</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter05</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala函数定义</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala03_FunctionDefine</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1. 函数1：无参，无返回值</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f1</span></span>(): <span class="type">Unit</span> = &#123; println(<span class="string">&quot;f1 ... ... 无参，无返回值&quot;</span>) &#125;</span><br><span class="line">    f1()</span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2. 函数2：无参，有返回值</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f2</span></span>(): <span class="type">String</span> = &#123;</span><br><span class="line">      println(<span class="string">&quot;f2 ... ... 无参，有返回值&quot;</span>)</span><br><span class="line">      <span class="string">&quot;f2&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">val</span> res2: <span class="type">String</span> = f2()</span><br><span class="line">    println(res2)</span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3. 函数3：有参，无返回值</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f3</span></span>(i: <span class="type">Int</span>): <span class="type">Unit</span> = &#123; println(<span class="string">&quot;f3 ... ... 有参，无返回值 &quot;</span> + i) &#125;</span><br><span class="line">    f3(<span class="number">12</span>)</span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 4. 函数4：有参，有返回值</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f4</span></span>(s: <span class="type">String</span>): <span class="type">String</span> = &#123;</span><br><span class="line">      <span class="string">&quot;f4 ... &quot;</span> + s + <span class="string">&quot; ... 有参，有返回值&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> res4: <span class="type">String</span> = f4(<span class="string">&quot;qaz&quot;</span>)</span><br><span class="line">    println(res4)</span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 5. 函数5：多参，无返回值</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f5</span></span>(a: <span class="type">String</span>, b: <span class="type">Int</span>): <span class="type">Unit</span> = &#123;</span><br><span class="line">      println(<span class="string">&quot;f5 ... ... 多参，无返回值&quot;</span>)</span><br><span class="line">      println(<span class="string">&quot;参数a = &quot;</span> + a + <span class="string">&quot;, 参数b = &quot;</span> + b)</span><br><span class="line">    &#125;</span><br><span class="line">    f5(<span class="string">&quot;asd&quot;</span>, <span class="number">123</span>)</span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 6. 函数6：多参，有返回值</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f6</span></span>(a: <span class="type">Int</span>, b: <span class="type">Int</span>): <span class="type">String</span> = &#123;</span><br><span class="line">      println(<span class="string">&quot;f6 ... ... 多参，有返回值&quot;</span>)</span><br><span class="line">      (a * b).toString</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> res6: <span class="type">String</span> = f6(<span class="number">12</span>, <span class="number">57</span>)</span><br><span class="line">    println(res6)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="四、函数参数"><a href="#四、函数参数" class="headerlink" title="四、函数参数"></a>四、函数参数</h2><p>1.可变参数</p>
<p>2.可变参数位置：如果参数列表中存在多个参数，那么可变参数一般放置在最后</p>
<p>3.参数默认值：一般将有默认值的参数放置在参数列表的后面</p>
<p>4.带名参数</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter05</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala函数参数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala04_FunctionParameter</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1. 可变参数</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f1</span></span>(name: <span class="type">String</span>*): <span class="type">Unit</span> = &#123;</span><br><span class="line">      println(name)</span><br><span class="line">    &#125;</span><br><span class="line">    f1(<span class="string">&quot;zhangsan&quot;</span>, <span class="string">&quot;lisi&quot;</span>, <span class="string">&quot;wangwu&quot;</span>) <span class="comment">// ArraySeq(zhangsan, lisi, wangwu)</span></span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2. 可变参数的位置：如果参数列表中存在多个参数，那么可变参数一般放置在最后</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f2</span></span>(age: <span class="type">Int</span>, name: <span class="type">String</span>*): <span class="type">Unit</span> = &#123;</span><br><span class="line">      println(name + <span class="string">&quot;, &quot;</span> + age)</span><br><span class="line">    &#125;</span><br><span class="line">    f2(<span class="number">15</span>, <span class="string">&quot;zhaoliu&quot;</span>, <span class="string">&quot;zhengqi&quot;</span>) <span class="comment">// ArraySeq(zhaoliu, zhengqi), 15</span></span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3. 参数默认值：一般将有默认值的参数放置在参数列表的后面</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f3</span></span>(name: <span class="type">String</span>, age: <span class="type">Int</span> = <span class="number">12</span>): <span class="type">Unit</span> = &#123;</span><br><span class="line">      println(name + <span class="string">&quot;, &quot;</span> + age)</span><br><span class="line">    &#125;</span><br><span class="line">    f3(<span class="string">&quot;sunba&quot;</span>, <span class="number">22</span>) <span class="comment">// sunba, 22</span></span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f4</span></span>(name: <span class="type">String</span>, age: <span class="type">Int</span> = <span class="number">12</span>): <span class="type">Unit</span> = &#123;</span><br><span class="line">      println(name + <span class="string">&quot;, &quot;</span> + age)</span><br><span class="line">    &#125;</span><br><span class="line">    f4(age = <span class="number">25</span>, name = <span class="string">&quot;hanjiu&quot;</span>) <span class="comment">// hanjiu, 25</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="五、函数的至简原则（重点！！！）"><a href="#五、函数的至简原则（重点！！！）" class="headerlink" title="五、函数的至简原则（重点！！！）"></a><code>五、函数的至简原则（重点！！！）</code></h2><p>函数至简原则：能省则省</p>
<h3 id="函数至简原则细节"><a href="#函数至简原则细节" class="headerlink" title="函数至简原则细节"></a>函数至简原则细节</h3><ol>
<li><p>return 可以省略，Scala 会使用函数体的最后一行代码作为返回值；</p>
</li>
<li><p>如果函数体只有一行代码，可以省略花括号；</p>
</li>
<li><p>返回值类型如果能够推断出来，那么可以省略返回值类型（:和返回值类型一起省略）；</p>
</li>
<li><p>如果有 return，则不能省略返回值类型，必须指定；</p>
</li>
<li><p>如果函数明确声明 Unit，那么即使函数体中使用 return 关键字也不起作用（无论返回什么类型数据，都是 Unit）；</p>
</li>
<li><p>Scala 如果期望是无返回值类型，可以省略等号，但是要加上{}；</p>
</li>
<li><p>如果函数无参，但是声明了参数列表，那么调用时，小括号，可加可不加；</p>
</li>
<li><p>如果函数没有参数列表，那么小括号可以省略，调用时小括号必须省略；</p>
</li>
<li><p>如果不关心名称，只关心逻辑处理，那么函数名（def）可以省略（匿名函数）；</p>
</li>
</ol>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter05</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala函数至简原则</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala05_FunctionSimplify</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f0</span> </span>(name: <span class="type">String</span>): <span class="type">String</span> = &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="string">&quot; f0 ... 返回值：&quot;</span> + name</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1. return 可以省略，Scala 会使用函数体的最后一行代码作为返回值；</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f1</span></span>(name: <span class="type">String</span>): <span class="type">String</span> = &#123;</span><br><span class="line">      <span class="comment">//最后一行表达式作为函数返回值</span></span><br><span class="line">      <span class="string">&quot; f1 ... 返回值：&quot;</span> + name</span><br><span class="line">    &#125;</span><br><span class="line">    println(f1(<span class="string">&quot;zhangsan&quot;</span>))</span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2. 如果函数体只有一行代码，可以省略花括号；</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f2</span></span>(name: <span class="type">String</span>): <span class="type">String</span> = <span class="string">&quot; f2 ... 返回值：&quot;</span> + name</span><br><span class="line">    println(f2(<span class="string">&quot;lisi&quot;</span>))</span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3. 返回值类型如果能够推断出来，那么可以省略返回值类型（:和返回值类型一起省略）；</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f3</span></span>(name: <span class="type">String</span>) = <span class="string">&quot; f3 ... 返回值：&quot;</span> + name</span><br><span class="line">    println(f3(<span class="string">&quot;wangwu&quot;</span>))</span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 4. 如果有 return，则不能省略返回值类型，必须指定；</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f4</span></span>(name: <span class="type">String</span>): <span class="type">String</span> = <span class="keyword">return</span> <span class="string">&quot; f4 ... 返回值：&quot;</span> + name</span><br><span class="line">    println(f4(<span class="string">&quot;zhaoliu&quot;</span>))</span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 5. 如果函数明确声明 Unit，那么即使函数体中使用 return 关键字也不起作用（无论返回什么类型数据，都是 Unit）；</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f5</span></span>(name: <span class="type">String</span>): <span class="type">Unit</span> = <span class="string">&quot; f5 ... 返回值：&quot;</span> + name</span><br><span class="line">    println(f5(<span class="string">&quot;zhengqi&quot;</span>)) <span class="comment">// 注意：这里打印()</span></span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 6. Scala 如果期望是无返回值类型，可以省略等号，但是要加上&#123;&#125;；</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f6</span></span>(name: <span class="type">String</span>) &#123; println(<span class="string">&quot; f4 ... 返回值：&quot;</span> + name) &#125;</span><br><span class="line">    println(f6(<span class="string">&quot;sunba&quot;</span>))</span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 7. 如果函数无参，但是声明了参数列表，那么调用时，小括号，可加可不加；</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f7</span></span>() &#123; println(<span class="string">&quot;f7 ... ...&quot;</span>) &#125;</span><br><span class="line">    f7 <span class="comment">// 括号可加可不加</span></span><br><span class="line">    f7()</span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 8. 如果函数没有参数列表，那么小括号可以省略，调用时小括号必须省略；</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f8</span> </span>&#123; println(<span class="string">&quot;f8 ... ...&quot;</span>) &#125;</span><br><span class="line">    f8</span><br><span class="line"><span class="comment">//    f8() // 函数定义没加参数列表的括号，调用时也不能加括号</span></span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 9. 如果不关心名称，只关心逻辑处理，那么函数名（def）可以省略（就是我们说的：匿名函数）；</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f9</span> </span>= (name: <span class="type">String</span>) =&gt; &#123; println(<span class="string">&quot;f9 ... &quot;</span> + name) &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 匿名函数，通常作为一个参数使用，如下所示</span></span><br><span class="line"><span class="comment">     * f10的参数是需要一个入参是String类型，无返回值（返回值为Unit）的函数</span></span><br><span class="line"><span class="comment">     * f10函数本身是无返回值（返回值为Unit）的</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f10</span></span>(f: <span class="type">String</span> =&gt; <span class="type">Unit</span>): <span class="type">Unit</span> = &#123;</span><br><span class="line">      f(<span class="string">&quot;zhangsan&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    f10(f9) <span class="comment">// f9 ... zhangsan</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 将 f10 调用与 f9 定义简化如下</span></span><br><span class="line"><span class="comment">     * f9不需要提前定义，直接在调用时定义</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    f10((name: <span class="type">String</span>) =&gt; &#123;</span><br><span class="line">      println(<span class="string">&quot;f10 ... ... &quot;</span> + name)</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="函数式编程-函数高级"><a href="#函数式编程-函数高级" class="headerlink" title="函数式编程-函数高级"></a>函数式编程-函数高级</h1><h2 id="一、高阶函数"><a href="#一、高阶函数" class="headerlink" title="一、高阶函数"></a>一、高阶函数</h2><h3 id="函数可以作为值进行传递"><a href="#函数可以作为值进行传递" class="headerlink" title="函数可以作为值进行传递"></a>函数可以作为值进行传递</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chpater06</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala函数可以作为值进行传递</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala01_TestFunction</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 定义一个函数，无参且返回Int类型数值1</span></span><br><span class="line"><span class="comment">   *</span></span><br><span class="line"><span class="comment">   * @return</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">foo</span></span>(): <span class="type">Int</span> = &#123;</span><br><span class="line">    println(<span class="string">&quot;foo ... ...&quot;</span>)</span><br><span class="line">    <span class="number">1</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 函数调用</span></span><br><span class="line"><span class="comment">     * 1、函数调用，先打印 foo ... ...</span></span><br><span class="line"><span class="comment">     * 2、函数返回值，赋值给f1</span></span><br><span class="line"><span class="comment">     * 3、打印数值1</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"><span class="comment">//    val f1 = foo</span></span><br><span class="line"><span class="comment">//    println(f1)</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">//将函数作为一个整体赋值给f2</span></span><br><span class="line">    <span class="comment">// 1、在 foo 后面加一个空格，再加一个下划线_，就是将foo函数整体赋值给了f2</span></span><br><span class="line">    <span class="comment">// 2、直接println(f2)，打印的是函数地址值（类似java的对象地址值）</span></span><br><span class="line">    <span class="keyword">val</span> f2 = foo _</span><br><span class="line">    println(f2)</span><br><span class="line">    <span class="comment">// 3、怎么调用函数f2呢？ f2() 就可以了</span></span><br><span class="line">    println(f2())</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">org.example.chpater06.<span class="type">Scala01_TestFunction</span>$$$<span class="type">Lambda</span>$<span class="number">3</span>/<span class="number">947679291</span>@<span class="number">224</span>aed64</span><br><span class="line">foo ... ...</span><br><span class="line"><span class="number">1</span></span><br></pre></td></tr></table></figure>

<h3 id="函数可以作为参数传递"><a href="#函数可以作为参数传递" class="headerlink" title="函数可以作为参数传递"></a>函数可以作为参数传递</h3><p><strong>重要思想：数据和逻辑分离</strong></p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chpater06</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala函数可以作为参数传递</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala02_TestFunction</span> </span>&#123;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 定义一个函数,定义一个两个整数操作的函数</span></span><br><span class="line"><span class="comment">   *</span></span><br><span class="line"><span class="comment">   * @param num1 Int类型整数</span></span><br><span class="line"><span class="comment">   * @param num2 Int类型整数</span></span><br><span class="line"><span class="comment">   * @param op 操作函数,具体怎么操作,需要将函数作为参数传入</span></span><br><span class="line"><span class="comment">   * @return</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">fun</span></span>(num1: <span class="type">Int</span>, num2: <span class="type">Int</span>, op: (<span class="type">Int</span>, <span class="type">Int</span>) =&gt; <span class="type">Int</span>): <span class="type">Int</span> = &#123;</span><br><span class="line">    op(num1, num2)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//调用</span></span><br><span class="line">    <span class="keyword">val</span> res1 = fun(<span class="number">1</span>, <span class="number">2</span>, (n: <span class="type">Int</span>, m: <span class="type">Int</span>) =&gt; &#123;</span><br><span class="line">      n + m</span><br><span class="line">    &#125;)</span><br><span class="line">    println(res1)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> res2 = fun(<span class="number">3</span>, <span class="number">5</span>, (n: <span class="type">Int</span>, m: <span class="type">Int</span>) =&gt; &#123;</span><br><span class="line">      m - n</span><br><span class="line">    &#125;)</span><br><span class="line">    println(res2)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="函数可以作为函数返回值返回"><a href="#函数可以作为函数返回值返回" class="headerlink" title="函数可以作为函数返回值返回"></a>函数可以作为函数返回值返回</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chpater06</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala函数可以作为函数返回值返回</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala03_TestFunction</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">f1</span></span>() = &#123;</span><br><span class="line">    println(<span class="string">&quot;f1 ... ...&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f2</span></span>(a: <span class="type">Int</span>) = &#123;</span><br><span class="line">      println(<span class="string">&quot;f2 ... ...&quot;</span> + a)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 函数最后一行作为函数返回值</span></span><br><span class="line">    <span class="comment">// 此处将f2函数作为返回值返回</span></span><br><span class="line">    f2 _</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 调用函数 f1,这时就是普通的函数调用,输出: f1 ... ...</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    f1</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 输出f1的结果,如上节调用的,f1函数返回值为一个函数,所以打印函数的地址值,打印如下</span></span><br><span class="line"><span class="comment">     * 注意:下面两个调用方式一样</span></span><br><span class="line"><span class="comment">     * f1 ... ...</span></span><br><span class="line"><span class="comment">     * org.example.chpater06.Scala03_TestFunction$$$Lambda$3/466002798@1ff8b8f</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    println(f1)</span><br><span class="line">    println(f1())</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 因为f1返回的是一个函数,所以可以接着调用,如下所示调用方式,打印结果如下:</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * f1 ... ...</span></span><br><span class="line"><span class="comment">     * f2 ... ...1</span></span><br><span class="line"><span class="comment">     * ()</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    println(f1()(<span class="number">1</span>))</span><br><span class="line">  &#125;  </span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 上面函数简写</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">func1</span></span>() = &#123;</span><br><span class="line">    println(<span class="string">&quot;f1 ... ...&quot;</span>)</span><br><span class="line">    println(<span class="string">&quot;f2 ... ...&quot;</span> + _)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>Tips</strong>：</p>
<ul>
<li>调用 f1() 后，函数 f1 将函数中定义的函数 f2() 返回了，所以打印的地址值为函数 f2() 的；</li>
<li>f1() 返回是一个函数本身，所以可以再次调用，就出现了 f1()(2)；</li>
</ul>
<h2 id="二、匿名函数"><a href="#二、匿名函数" class="headerlink" title="二、匿名函数"></a>二、匿名函数</h2><h3 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(参数: 返回值) =&gt; &#123;函数体&#125;</span><br></pre></td></tr></table></figure>

<h3 id="传递匿名函数至简原则"><a href="#传递匿名函数至简原则" class="headerlink" title="传递匿名函数至简原则"></a>传递匿名函数至简原则</h3><ol>
<li><p>参数的类型可以省略，会根据形参进行自动的推导</p>
</li>
<li><p>类型省略之后，发现只有一个参数，则圆括号可以省略；其他情况：没有参数和参数超过1的永远不能省略圆括号。</p>
</li>
<li><p>匿名函数如果只有一行，则大括号也可以省略</p>
</li>
<li><p>如果参数只出现一次，则参数省略且后面参数可以用_代替</p>
</li>
</ol>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chpater06</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala匿名函数和函数的至简原则</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala04_TestFunction</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 定义一个函数</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @param arr Int类型的集合</span></span><br><span class="line"><span class="comment">     * @param op  函数，入参Int类型，返回值Int类型</span></span><br><span class="line"><span class="comment">     * @return</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f</span></span>(arr: <span class="type">Array</span>[<span class="type">Int</span>], op: <span class="type">Int</span> =&gt; <span class="type">Int</span>): <span class="type">Array</span>[<span class="type">Int</span>] = &#123;</span><br><span class="line">      <span class="keyword">for</span> (elem &lt;- arr) <span class="keyword">yield</span> op(elem)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 定义一个逻辑函数</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @param num Int类型数值</span></span><br><span class="line"><span class="comment">     * @return 返回计算后的数值</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">op</span></span>(num: <span class="type">Int</span>): <span class="type">Int</span> = &#123;</span><br><span class="line">      num + <span class="number">1</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//调用</span></span><br><span class="line">    <span class="keyword">val</span> ints: <span class="type">Array</span>[<span class="type">Int</span>] = f(<span class="type">Array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>), op)</span><br><span class="line">    println(ints.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line"></span><br><span class="line">    println(<span class="string">&quot; === === === === === === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 函数至简原则</span></span><br><span class="line">    <span class="comment">// 以上面 函数f 为例，进行简化调用</span></span><br><span class="line">    <span class="comment">// 标准调用</span></span><br><span class="line">    <span class="keyword">val</span> arr1 = f(<span class="type">Array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>), (num: <span class="type">Int</span>) =&gt; &#123;</span><br><span class="line">      num + <span class="number">1</span></span><br><span class="line">    &#125;)</span><br><span class="line">    println(<span class="string">&quot;标准调用：&quot;</span> + arr1.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1. 参数的类型可以省略，会根据形参进行自动的推导</span></span><br><span class="line">    <span class="keyword">val</span> arr2 = f(<span class="type">Array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>), (num) =&gt; &#123;</span><br><span class="line">      num + <span class="number">1</span></span><br><span class="line">    &#125;)</span><br><span class="line">    println(<span class="string">&quot;标准调用：&quot;</span> + arr2.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2. 类型省略之后，发现只有一个参数，则圆括号可以省略；其他情况：没有参数和参数超过1的永远不能省略圆括号。</span></span><br><span class="line">    <span class="keyword">val</span> arr3 = f(<span class="type">Array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>), num =&gt; &#123;</span><br><span class="line">      num + <span class="number">1</span></span><br><span class="line">    &#125;)</span><br><span class="line">    println(<span class="string">&quot;标准调用：&quot;</span> + arr3.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3. 匿名函数如果只有一行，则大括号也可以省略</span></span><br><span class="line">    <span class="keyword">val</span> arr4 = f(<span class="type">Array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>), num =&gt; num + <span class="number">1</span>)</span><br><span class="line">    println(<span class="string">&quot;标准调用：&quot;</span> + arr4.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 4. 如果参数只出现一次，则参数省略且后面参数可以用_代替</span></span><br><span class="line">    <span class="keyword">val</span> arr5 = f(<span class="type">Array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>), _ + <span class="number">1</span>)</span><br><span class="line">    println(<span class="string">&quot;标准调用：&quot;</span> + arr5.mkString(<span class="string">&quot;, &quot;</span>))</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 5. 如果可以推断出当前传入 println 是一个函数体，而不是调用语句，可以直接省略下划线</span></span><br><span class="line">    f2((name: <span class="type">String</span>) =&gt; &#123;</span><br><span class="line">      println(name)</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="comment">//简化如下</span></span><br><span class="line">    f2(println)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">f2</span></span>(op: <span class="type">String</span> =&gt; <span class="type">Unit</span>): <span class="type">Unit</span> = &#123;</span><br><span class="line">    op(<span class="string">&quot;hello scala&quot;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="传递有两个参数的匿名函数"><a href="#传递有两个参数的匿名函数" class="headerlink" title="传递有两个参数的匿名函数"></a>传递有两个参数的匿名函数</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chpater06</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala传递两个参数的匿名函数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala05_TestFunction</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 定义一个函数</span></span><br><span class="line"><span class="comment">   *</span></span><br><span class="line"><span class="comment">   * @param a Int类型的整数</span></span><br><span class="line"><span class="comment">   * @param b Int类型的整数</span></span><br><span class="line"><span class="comment">   * @param op 操作函数，参数为两个Int类型的值，返回Int类型的值</span></span><br><span class="line"><span class="comment">   * @return</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">f</span></span>(a: <span class="type">Int</span>, b: <span class="type">Int</span>, op: (<span class="type">Int</span>, <span class="type">Int</span>) =&gt; <span class="type">Int</span>): <span class="type">Int</span> = &#123;</span><br><span class="line">    op(a, b)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="comment">// 调用</span></span><br><span class="line">    <span class="keyword">val</span> i1: <span class="type">Int</span> = f(<span class="number">2</span>, <span class="number">3</span>, (x: <span class="type">Int</span>, y: <span class="type">Int</span>) =&gt; &#123; x + y &#125;)</span><br><span class="line">    println(i1)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 参数类型可以省略，会根据形参进行自动推导</span></span><br><span class="line">    <span class="comment">// 如果只有一行，则大括号可以省略</span></span><br><span class="line">    <span class="keyword">val</span> i2: <span class="type">Int</span> = f(<span class="number">3</span>, <span class="number">4</span>, (x, y) =&gt; x + y)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 如果参数只出现一次，则参数省略且后面参数可以使用_代替</span></span><br><span class="line">    <span class="keyword">val</span> i3: <span class="type">Int</span> = f(<span class="number">5</span>, <span class="number">6</span>, _ + _)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="匿名函数案例"><a href="#匿名函数案例" class="headerlink" title="匿名函数案例"></a>匿名函数案例</h3><p><strong>模拟Map映射，Filter过滤，Reduce聚合</strong></p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// map</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">map</span></span>(arr:<span class="type">Array</span>[<span class="type">Int</span>], op:<span class="type">Int</span>=&gt;<span class="type">Int</span>)=&#123;</span><br><span class="line">    <span class="keyword">for</span> (elem &lt;- arr) <span class="keyword">yield</span>  op(elem)</span><br><span class="line">&#125;</span><br><span class="line">println(map(<span class="type">Array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>), _ + <span class="number">2</span>).mkString(<span class="string">&quot;,&quot;</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment">// filter</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">filter</span></span>(arr: <span class="type">Array</span>[<span class="type">Int</span>], op:<span class="type">Int</span>=&gt;<span class="type">Boolean</span>)=&#123;</span><br><span class="line">    <span class="keyword">for</span> (elem &lt;- arr) <span class="keyword">yield</span> op(elem)</span><br><span class="line">&#125;</span><br><span class="line">println(filter(<span class="type">Array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>), _ % <span class="number">2</span> == <span class="number">1</span>).mkString(<span class="string">&quot;,&quot;</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment">// reduce</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">reduce</span></span>(arr:<span class="type">Array</span>[<span class="type">Int</span>], op:(<span class="type">Int</span>,<span class="type">Int</span>)=&gt;<span class="type">Int</span>)=&#123;</span><br><span class="line">    <span class="keyword">var</span> init:<span class="type">Int</span> = arr(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (elem &lt;- <span class="number">1</span> until arr.length) &#123;</span><br><span class="line">        init = op(init, arr(elem))</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    init</span><br><span class="line">&#125;</span><br><span class="line">println(reduce(<span class="type">Array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>), _ + _))</span><br></pre></td></tr></table></figure>

<h2 id="三、函数柯里化和闭包"><a href="#三、函数柯里化和闭包" class="headerlink" title="三、函数柯里化和闭包"></a>三、函数柯里化和闭包</h2><h3 id="概念说明"><a href="#概念说明" class="headerlink" title="概念说明"></a>概念说明</h3><p><strong>闭包</strong>：如果一个函数，访问到了它的外部（局部）变量的值，那么这个函数和他所处的环境，称为闭包</p>
<p><strong>函数柯里化</strong>：把一个参数列表的多个参数，变成多个参数列表。</p>
<p><strong>注：闭包生成的class文件会多一个文件；</strong></p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chpater06</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala函数柯里化和闭包</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala07_ClosureAndCurrying</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 闭包</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @param a 入参, Int类型的整数</span></span><br><span class="line"><span class="comment">     * @return 返回值为一个函数, 函数的入参Int, 返回值Int</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f1</span></span>(a: <span class="type">Int</span>): <span class="type">Int</span> =&gt; <span class="type">Int</span> = &#123;</span><br><span class="line">      <span class="comment">// 作为函数f2而言, 在f2内部使用到了函数外部的变量a</span></span><br><span class="line">      <span class="function"><span class="keyword">def</span> <span class="title">f2</span></span>(b: <span class="type">Int</span>): <span class="type">Int</span> = &#123;</span><br><span class="line">        a + b</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 返回f2函数</span></span><br><span class="line">      f2 _</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 调用</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    println(f1(<span class="number">1</span>)(<span class="number">5</span>))</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 上面函数f1实现有两层逻辑,相当于两个函数进行了个嵌套</span></span><br><span class="line"><span class="comment">     * 换个写法,将多层嵌套的参数,写到最处层函数参数列表上,通过多个参数列表表示,如下所示</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f3</span></span>(a: <span class="type">Int</span>)(b: <span class="type">Int</span>): <span class="type">Int</span> = a + b</span><br><span class="line">      </span><br><span class="line">    println(f3(<span class="number">1</span>)(<span class="number">2</span>))</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="四、递归"><a href="#四、递归" class="headerlink" title="四、递归"></a>四、递归</h2><ol>
<li>方法调用自身</li>
<li>方法必须要有跳出的逻辑</li>
<li>方法调用自身时，传递的参数应该有规律</li>
<li><strong>Scala中的递归必须声明函数返回值类型</strong></li>
</ol>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chpater06</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala递归</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala08_Recursion</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="keyword">val</span> res: <span class="type">Int</span> = recursion(<span class="number">5</span>)</span><br><span class="line">    println(res)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">val</span> res1: <span class="type">Int</span> = recursion1(<span class="number">5</span>)</span><br><span class="line">    println(res1)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 递归实现阶乘</span></span><br><span class="line"><span class="comment">   *</span></span><br><span class="line"><span class="comment">   * @param num</span></span><br><span class="line"><span class="comment">   * @return</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="comment">//函数必须声明返回值</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">recursion</span></span>(num: <span class="type">Int</span>): <span class="type">Int</span> = &#123;</span><br><span class="line">    <span class="keyword">if</span> (num == <span class="number">1</span>) &#123;</span><br><span class="line">      <span class="comment">//方法必须有跳出逻辑,当前示例返回1跳出</span></span><br><span class="line">      <span class="number">1</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="comment">//调用自身</span></span><br><span class="line">      recursion(num - <span class="number">1</span>) * num</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 尾递归调用</span></span><br><span class="line"><span class="comment">   * 尾递归是指递归函数在递归调用时，最后一步是返回递归函数本身的情况。</span></span><br><span class="line"><span class="comment">   * 在这种情况下，编译器或解释器可以优化代码，使其只需保留一个堆栈帧，而不是像普通递归那样需要保留每一步的调用信息，从而减少内存消耗和提高性能。</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">recursion1</span></span>(num: <span class="type">Int</span>): <span class="type">Int</span> = &#123;</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">loop</span></span>(num: <span class="type">Int</span>, currRes: <span class="type">Int</span>): <span class="type">Int</span> = &#123;</span><br><span class="line">      <span class="keyword">if</span> (num == <span class="number">1</span>) &#123;</span><br><span class="line">        currRes</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        loop(num - <span class="number">1</span>, currRes * num)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    loop(num, <span class="number">1</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="五、控制抽象"><a href="#五、控制抽象" class="headerlink" title="五、控制抽象"></a>五、控制抽象</h2><h3 id="值调用：把计算后的值传递过去"><a href="#值调用：把计算后的值传递过去" class="headerlink" title="值调用：把计算后的值传递过去"></a>值调用：把计算后的值传递过去</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chpater06</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala控制抽象</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala09_ControlAbstraction1</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 1、传值参数</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">foo</span></span>(a: <span class="type">Int</span>): <span class="type">Unit</span> = &#123;</span><br><span class="line">    println(<span class="string">&quot;a = &quot;</span> + a)</span><br><span class="line">    println(<span class="string">&quot;a = &quot;</span> + a)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">f1</span></span>(): <span class="type">Int</span> = &#123;</span><br><span class="line">    println(<span class="string">&quot;f1 ... ... &quot;</span>)</span><br><span class="line">    <span class="number">12</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"><span class="comment">//    foo(23)</span></span><br><span class="line">    foo(f1())</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//打印结果</span></span><br><span class="line">f1 ... ... </span><br><span class="line">a = <span class="number">12</span></span><br><span class="line">a = <span class="number">12</span></span><br></pre></td></tr></table></figure>

<h3 id="名调用：把代码传递过去"><a href="#名调用：把代码传递过去" class="headerlink" title="名调用：把代码传递过去"></a>名调用：把代码传递过去</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chpater06</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala控制抽象</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 名调用：把代码传递</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala10_ControlAbstraction2</span> </span>&#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">//  def foo(a: Int): Unit = &#123;</span></span><br><span class="line">  <span class="comment">// 本质：传递函数代码</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">foo</span></span>(a: =&gt; <span class="type">Int</span>): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="comment">// 这就是调用a，并打印了返回值</span></span><br><span class="line">    println(<span class="string">&quot;a = &quot;</span> + a)</span><br><span class="line">    println(<span class="string">&quot;a = &quot;</span> + a)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">f</span></span>: () =&gt; <span class="type">Int</span> = () =&gt; &#123;</span><br><span class="line">      println(<span class="string">&quot;f ... ...&quot;</span>)</span><br><span class="line">      <span class="number">10</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    foo(f())</span><br><span class="line">    </span><br><span class="line">    println(<span class="string">&quot; === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//合并简写</span></span><br><span class="line">    foo(&#123;</span><br><span class="line">      println(<span class="string">&quot;这是一个代码块&quot;</span>)</span><br><span class="line">      <span class="number">19</span></span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//打印结果</span></span><br><span class="line">f ... ...</span><br><span class="line">a = <span class="number">10</span></span><br><span class="line">f ... ...</span><br><span class="line">a = <span class="number">10</span></span><br><span class="line"> === === === === === === </span><br><span class="line">这是一个代码块</span><br><span class="line">a = <span class="number">19</span></span><br><span class="line">这是一个代码块</span><br><span class="line">a = <span class="number">19</span></span><br></pre></td></tr></table></figure>

<h3 id="自定义while"><a href="#自定义while" class="headerlink" title="自定义while"></a>自定义while</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chpater06</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala控制抽象，自定义while函数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala11_DefWhile</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">myWhile</span></span>(condition: =&gt; <span class="type">Boolean</span>)(op: =&gt; <span class="type">Unit</span>): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="keyword">if</span> (condition) &#123;</span><br><span class="line">      op</span><br><span class="line">      myWhile(condition)(op)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="keyword">var</span> i: <span class="type">Int</span> = <span class="number">1</span></span><br><span class="line">    myWhile(i &lt; <span class="number">10</span>) &#123;</span><br><span class="line">      println(i)</span><br><span class="line">      i += <span class="number">1</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="六、惰性函数"><a href="#六、惰性函数" class="headerlink" title="六、惰性函数"></a>六、惰性函数</h2><p>当函数返回值被声明为lazy时，函数的执行将被推迟，直到我们首次对此取值，该函数才会执行。这种函数我们称之为惰性函数。</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chpater06</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala惰性函数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala12_Lazy</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="keyword">lazy</span> <span class="keyword">val</span> res = sum(<span class="number">10</span>, <span class="number">30</span>)</span><br><span class="line">    println(<span class="string">&quot;=== === === === === ===&quot;</span>)</span><br><span class="line">    println(<span class="string">&quot;res = &quot;</span> + res)</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">sum</span></span>(n1: <span class="type">Int</span>, n2: <span class="type">Int</span>): <span class="type">Int</span> = &#123;</span><br><span class="line">    println(<span class="string">&quot;sum ... 执行 ...&quot;</span>)</span><br><span class="line">    n1 + n2</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 结果：</span></span><br><span class="line">--- --- --- ---</span><br><span class="line">=== === === === === ===</span><br><span class="line">sum ... 执行 ...</span><br><span class="line">res = <span class="number">40</span></span><br></pre></td></tr></table></figure>

<p><strong>注：lazy不能修饰var类型变量；</strong></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Oct25-X</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/">https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://oct25-x.atomgit.net/oct25-xxxxx" target="_blank">Oct25-X</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/oct25-xxxxx/tags/Scala/">Scala</a></div><div class="post_share"><div class="social-share" data-image="/oct25-xxxxx/img/cover_img/ctt48.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/"><img class="prev-cover" src="/oct25-xxxxx/img/cover_img/ctt49.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">大数据之多范式编程语言 Scala06_面向对象</div></div></a></div><div class="next-post pull-right"><a href="/oct25-xxxxx/2024/02/20/47%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala04_%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/"><img class="next-cover" src="/oct25-xxxxx/img/cover_img/ctt47.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">大数据之多范式编程语言 Scala04_流程控制</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/oct25-xxxxx/2024/02/18/44%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala01_%E5%85%A5%E9%97%A8/" title="大数据之多范式编程语言 Scala01_入门"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt44.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-18</div><div class="title">大数据之多范式编程语言 Scala01_入门</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/19/46%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala03_%E8%BF%90%E7%AE%97%E7%AC%A6/" title="大数据之多范式编程语言 Scala03_运算符"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt46.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-19</div><div class="title">大数据之多范式编程语言 Scala03_运算符</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/18/45%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala02_%E5%8F%98%E9%87%8F%E5%92%8C%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/" title="大数据之多范式编程语言 Scala02_变量和数据类型"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt45.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-18</div><div class="title">大数据之多范式编程语言 Scala02_变量和数据类型</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/20/47%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala04_%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/" title="大数据之多范式编程语言 Scala04_流程控制"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt47.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-20</div><div class="title">大数据之多范式编程语言 Scala04_流程控制</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt49.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-21</div><div class="title">大数据之多范式编程语言 Scala06_面向对象</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt50.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-22</div><div class="title">大数据之多范式编程语言 Scala07_集合</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Oct25-X</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B-%E5%87%BD%E6%95%B0%E5%9F%BA%E7%A1%80"><span class="toc-number">1.</span> <span class="toc-text">函数式编程-函数基础</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E5%87%BD%E6%95%B0%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95"><span class="toc-number">1.1.</span> <span class="toc-text">一、函数基本语法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%87%BD%E6%95%B0%E5%92%8C%E6%96%B9%E6%B3%95%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-number">1.2.</span> <span class="toc-text">二、函数和方法的区别</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5"><span class="toc-number">1.2.1.</span> <span class="toc-text">核心概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%92%8C%E6%96%B9%E6%B3%95%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.2.2.</span> <span class="toc-text">函数和方法示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%87%BD%E6%95%B0%E5%AE%9A%E4%B9%89"><span class="toc-number">1.3.</span> <span class="toc-text">三、函数定义</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%AE%9A%E4%B9%89"><span class="toc-number">1.3.1.</span> <span class="toc-text">函数定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%AE%9A%E4%B9%89%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.3.2.</span> <span class="toc-text">函数定义示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E5%87%BD%E6%95%B0%E5%8F%82%E6%95%B0"><span class="toc-number">1.4.</span> <span class="toc-text">四、函数参数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E5%87%BD%E6%95%B0%E7%9A%84%E8%87%B3%E7%AE%80%E5%8E%9F%E5%88%99%EF%BC%88%E9%87%8D%E7%82%B9%EF%BC%81%EF%BC%81%EF%BC%81%EF%BC%89"><span class="toc-number">1.5.</span> <span class="toc-text">五、函数的至简原则（重点！！！）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E8%87%B3%E7%AE%80%E5%8E%9F%E5%88%99%E7%BB%86%E8%8A%82"><span class="toc-number">1.5.1.</span> <span class="toc-text">函数至简原则细节</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B-%E5%87%BD%E6%95%B0%E9%AB%98%E7%BA%A7"><span class="toc-number">2.</span> <span class="toc-text">函数式编程-函数高级</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E9%AB%98%E9%98%B6%E5%87%BD%E6%95%B0"><span class="toc-number">2.1.</span> <span class="toc-text">一、高阶函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%8F%AF%E4%BB%A5%E4%BD%9C%E4%B8%BA%E5%80%BC%E8%BF%9B%E8%A1%8C%E4%BC%A0%E9%80%92"><span class="toc-number">2.1.1.</span> <span class="toc-text">函数可以作为值进行传递</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%8F%AF%E4%BB%A5%E4%BD%9C%E4%B8%BA%E5%8F%82%E6%95%B0%E4%BC%A0%E9%80%92"><span class="toc-number">2.1.2.</span> <span class="toc-text">函数可以作为参数传递</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%8F%AF%E4%BB%A5%E4%BD%9C%E4%B8%BA%E5%87%BD%E6%95%B0%E8%BF%94%E5%9B%9E%E5%80%BC%E8%BF%94%E5%9B%9E"><span class="toc-number">2.1.3.</span> <span class="toc-text">函数可以作为函数返回值返回</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0"><span class="toc-number">2.2.</span> <span class="toc-text">二、匿名函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AF%AD%E6%B3%95"><span class="toc-number">2.2.1.</span> <span class="toc-text">语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BC%A0%E9%80%92%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0%E8%87%B3%E7%AE%80%E5%8E%9F%E5%88%99"><span class="toc-number">2.2.2.</span> <span class="toc-text">传递匿名函数至简原则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BC%A0%E9%80%92%E6%9C%89%E4%B8%A4%E4%B8%AA%E5%8F%82%E6%95%B0%E7%9A%84%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0"><span class="toc-number">2.2.3.</span> <span class="toc-text">传递有两个参数的匿名函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0%E6%A1%88%E4%BE%8B"><span class="toc-number">2.2.4.</span> <span class="toc-text">匿名函数案例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%87%BD%E6%95%B0%E6%9F%AF%E9%87%8C%E5%8C%96%E5%92%8C%E9%97%AD%E5%8C%85"><span class="toc-number">2.3.</span> <span class="toc-text">三、函数柯里化和闭包</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A6%82%E5%BF%B5%E8%AF%B4%E6%98%8E"><span class="toc-number">2.3.1.</span> <span class="toc-text">概念说明</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E9%80%92%E5%BD%92"><span class="toc-number">2.4.</span> <span class="toc-text">四、递归</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E6%8E%A7%E5%88%B6%E6%8A%BD%E8%B1%A1"><span class="toc-number">2.5.</span> <span class="toc-text">五、控制抽象</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%80%BC%E8%B0%83%E7%94%A8%EF%BC%9A%E6%8A%8A%E8%AE%A1%E7%AE%97%E5%90%8E%E7%9A%84%E5%80%BC%E4%BC%A0%E9%80%92%E8%BF%87%E5%8E%BB"><span class="toc-number">2.5.1.</span> <span class="toc-text">值调用：把计算后的值传递过去</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%8D%E8%B0%83%E7%94%A8%EF%BC%9A%E6%8A%8A%E4%BB%A3%E7%A0%81%E4%BC%A0%E9%80%92%E8%BF%87%E5%8E%BB"><span class="toc-number">2.5.2.</span> <span class="toc-text">名调用：把代码传递过去</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89while"><span class="toc-number">2.5.3.</span> <span class="toc-text">自定义while</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AD%E3%80%81%E6%83%B0%E6%80%A7%E5%87%BD%E6%95%B0"><span class="toc-number">2.6.</span> <span class="toc-text">六、惰性函数</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"><img src="/oct25-xxxxx/img/cover_img/ctt52.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型">大数据之多范式编程语言 Scala09_隐式转换、异常和泛型</a><time datetime="2024-02-24T04:55:34.000Z" title="发表于 2024-02-24 12:55:34">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配"><img src="/oct25-xxxxx/img/cover_img/ctt51.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala08_模式匹配"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配">大数据之多范式编程语言 Scala08_模式匹配</a><time datetime="2024-02-24T00:46:12.000Z" title="发表于 2024-02-24 08:46:12">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合"><img src="/oct25-xxxxx/img/cover_img/ctt50.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala07_集合"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合">大数据之多范式编程语言 Scala07_集合</a><time datetime="2024-02-22T08:16:10.000Z" title="发表于 2024-02-22 16:16:10">2024-02-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象"><img src="/oct25-xxxxx/img/cover_img/ctt49.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala06_面向对象"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象">大数据之多范式编程语言 Scala06_面向对象</a><time datetime="2024-02-21T07:01:10.000Z" title="发表于 2024-02-21 15:01:10">2024-02-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程"><img src="/oct25-xxxxx/img/cover_img/ctt48.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala05_函数式编程"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程">大数据之多范式编程语言 Scala05_函数式编程</a><time datetime="2024-02-20T12:05:11.000Z" title="发表于 2024-02-20 20:05:11">2024-02-20</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg22.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By Oct25-X</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div id="rightMenu"><div class="rightMenu-group rightMenu-small"><div class="rightMenu-item" id="menu-backward"><i class="fa-solid fa-arrow-left"></i></div><div class="rightMenu-item" id="menu-forward"><i class="fa-solid fa-arrow-right"></i></div><div class="rightMenu-item" id="menu-refresh"><i class="fa-solid fa-arrow-rotate-right"></i></div><div class="rightMenu-item" id="menu-home"><i class="fa-solid fa-house"></i></div></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item menu-link" href="/archives/"><i class="fa-solid fa-archive"></i><span>文章归档</span></a><a class="rightMenu-item menu-link" href="/categories/"><i class="fa-solid fa-folder-open"></i><span>文章分类</span></a><a class="rightMenu-item menu-link" href="/tags/"><i class="fa-solid fa-tags"></i><span>文章标签</span></a></div><div class="rightMenu-group rightMenu-line rightMenuNormal"><a class="rightMenu-item" id="menu-translate"><i class="fa-solid fa-earth-asia"></i><span>繁简切换</span></a><div class="rightMenu-item" id="menu-darkmode"><i class="fa-solid fa-moon"></i><span>切换模式</span></div></div></div><div id="rightmenu-mask"></div><div><script src="/oct25-xxxxx/js/utils.js"></script><script src="/oct25-xxxxx/js/main.js"></script><script src="/oct25-xxxxx/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/oct25-xxxxx/js/search/local-search.js"></script><div class="js-pjax"></div><script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script><script defer data-pjax src="/js/rightMenu.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>